<template>
  <div class="container">

    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">

      <el-form-item>
        <el-input placeholder="讲师名" v-model="teacherQuery.name"/>
      </el-form-item>

      <el-form-item>
        <el-select placeholder="讲师头衔" clearable v-model="teacherQuery.level">
          <el-option label="首席讲师" value="2"></el-option>
          <el-option label="高级讲师" value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-date-picker type="datetime"
                        v-model="teacherQuery.begin"
                        placeholder="开始时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        default-time="00:00:00">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-date-picker
          type="datetime"
          v-model="teacherQuery.end"
          placeholder="截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="cleanInfo()">清空</el-button>
    </el-form>

    <!--表格-->
    <el-table :data="list" border highlight-current-row v-loading="listLoading" fit style="width: 100%;">
      <el-table-column type="index" label="序号" align="center" width="80"></el-table-column>
      <el-table-column prop="name" label="姓名" align="center" width="100"></el-table-column>
      <el-table-column label="头衔" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.level === 1 ? '高级讲师' : '首席讲师' }}
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="讲师简介" align="center" width="180"></el-table-column>
      <el-table-column prop="career" label="资历" align="center" width="180"></el-table-column>
      <el-table-column prop="gmtCreate" label="添加日期" align="center" width="160"></el-table-column>
      <el-table-column prop="sort" label="排序" width="60"/>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="warning" icon="el-icon-warning" size="mini" @click="updateTeacher(scope.row.id)">修改
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeTeacher(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>

<script>
import { getTeacherList } from '@/api/edu/teacher'
import teacher from '@/api/edu/teacher'

export default {
  name: 'List',
  data() {
    return {
      listLoading: true,
      list: [], // 数据列表
      page: 1, // 当前页码
      limit: 2, // 每页记录数
      total: 0, // 总页数
      teacherQuery: { // 查询条件
        name: '', //
        // intro: '',
        // career: '',
        level: '',
        gmtCreate: ''
      }
    }
  },
  created() {  // 当页面加载时获取数据
    // this.getList()
  },
  mounted() {
    this.getList()
  },
  methods: {

    // 分页查询讲师
    getList(page = 1) {
      this.page = page
      this.listLoading = true
      getTeacherList(this.page, this.limit, this.teacherQuery)
        .then(response => {
          // console.log(response)
          this.list = response.data.rows
          this.total = response.data.total
        })
        .catch(error => {
          console.log(error)
        })
      this.listLoading = false
    },

    // 删除讲师
    removeTeacher(id) {

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacher.removeTeacherById(id)
          .then(() => {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getList()
            }
          )
          .catch(error => {
            console.log(error)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })

    },
    onSubmit() {
      console.log('submit!')
    },

    // 清空输入信息
    cleanInfo() {
      this.teacherQuery = {}
      this.getList()
    },

    // 修改讲师信息跳转页面
    async updateTeacher(id) {
      // console.log(id)
      const result = await teacher.selectTeacherById(id)
      // console.log(result)
      this.$router.push({ path: '/teacher/save', query: { 'id': id } })
    }

  }
}
</script>

<style lang="less" scoped>
.el-table-column {

}

.demo-form-inline {
  margin-top: 40px;
  margin-left: 10px;
}
</style>
